<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\admin\model\BatteryBanner as BannerModel;
// 公告控制
class BatteryBanner extends Common
{
    
    // banner 公告列表
    public function getBannerList()
    {

        $keyword = request()->param('keyword', '');
        $order_type =request()->param('order_type', 1);
        $is_show=input('is_show','all');

        $this->orderList($keyword,$order_type,$is_show);

        $this->assign([
            'keyword' => $keyword,
            'order_type'=>$order_type,
            'is_show'=>$is_show,
        ]);

        return  $this->fetch('bannerList');
    }

    private function orderList($keyword = "" ,$order_type=1,$is_show='all')
    {

        $where = '';
        if ($keyword) {
            $where['title'] = ['like', "%$keyword%"];
        }

        if($order_type == 1){
            $order_type = 'add_time desc';
        }else{
            $order_type = 'add_time ';
        }
        
        if($is_show == 1){
            $where['is_show']=1;
        }else if($is_show == 2){
            $where['is_show'] = 2;
        }

        $list = Db::name(BannerModel::TABLE_NAME)->where($where)->order($order_type)->paginate(10);

        $page = $list->render();
        $banner_amount = Db(BannerModel::TABLE_NAME)->count();


        $this->assign([
            'page' => $page,
            'list' => $list,
            'banner_amount' => $banner_amount
        ]);

    }

    // 新建banner消息
    public function getBannerAdd(){
        return $this->fetch('bannerAdd');
    }

    public function postBannerAdd(Request $request)
    {
        if ($request->isPost()) {

            $date = input('post.');
            // 图片
            $file = $request->file('banner_img');
            if ($file) {
                
                $date['banner_img'] = handle_upload_image($file,BannerModel::BANNER_IMAGE_PATH) ;

            } else{

                $this->error('图片上传失败','BannerAdd');
            }

            $date['add_time']=time();

            $bool = Db::name(BannerModel::TABLE_NAME)->insert($date);

            if ($bool) {
                 $this->success('添加成功', 'BannerList');
            } else {
                $this->error('添加失败', 'BannerAdd');
            }
        }
    }

    // 修改banner 
    public function getBannerEdit(){
        $id=input('id');

        $bannerInfo= BannerModel::getBannerInfoById($id);

        return $this->fetch('bannerEdit',['info'=>BannerModel::handleBannerInfo($bannerInfo)]);

    }

    public function postBannerEdit()
    {
        if (request()->isPost()) {

            $banner_id= input('post.banner_id');
            $data['title'] = input('post.title');
            $data['describe'] = input('post.describe');
            $data['is_show'] = input('post.is_show');
            // 图片

            
            $file = request()->file('banner_img');
            if ($file) {
                
               $data['banner_img'] = handle_upload_image($file,BannerModel::BANNER_IMAGE_PATH) ;


                // 原来图片地址
                $oldImgPath =BannerModel::getBannerInfoById($banner_id, 'banner_img');


            } 

            $data['add_time']=time();
            $bool = Db::table(BannerModel::TABLE_NAME)->where('banner_id',$banner_id)->update($data);

            if ($bool) {
                if (isset($oldImgPath) && !empty($oldImgPath['banner_img'])) {
                    unlink(get_image_url(BannerModel::BANNER_IMAGE_PATH,$oldImgPath['banner_img'],false));

                }
                 $this->success('修改成功', '/battery_banner/BannerList');
            } else {
                $this->error('修改失败', '/battery_banner/BannerEdit/id/'.$banner_id);
            }
        }
    }

    // 修改上线状态
    public function postAjaxChangeStatus(){
        if(request()->isAjax()){
            $id = input('post.id');
            $status = input('post.status');
            $res = BannerModel::changeStatus($id, $status);

            return $res;
        }
    }

}
